package pkg.drowne.org;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class CreateGame extends Activity implements OnClickListener, Runnable {
	
	// Preference file
	public static final String PREF_FILE = "pf2";
	private SharedPreferences preferences;

	// UI
	RadioGroup timesetupRadioGroup;
	RadioButton radio05min, radio10min, radio15min;
	ImageButton timesetupButton;
	
	// game logic
	private String username="";
	private int time = 10, 
				answer=-1;
	
	private ProgressDialog pd;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.creategame);
        
        registerViews();
        setupOnClickListeners();
        loadPrefs();
        
    }

    private void loadPrefs() {
		
    	preferences = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
    	username = preferences.getString("username", "");
		
	}
    
    private boolean setPrefsHost() {
		
    	// get the preferences file ready
		preferences = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
		//preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		
		// saves values in the preferences file
		preferences.edit().putBoolean("hostPlayer", true).commit();
		preferences.edit().putBoolean("clientPlayer", false).commit();
		preferences.edit().putInt("idgame", answer).commit();
		
		return true;
	}

	private void registerViews() {
		
    	timesetupRadioGroup = (RadioGroup) this.findViewById(R.id.radio_group_match_time);
    	radio05min =  (RadioButton) this.findViewById(R.id.rbtn_time_05_min);
    	radio10min =  (RadioButton) this.findViewById(R.id.rbtn_time_10_min);
    	radio15min =  (RadioButton) this.findViewById(R.id.rbtn_time_15_min);
    	timesetupButton = (ImageButton) this.findViewById(R.id.time_setup_button);
	}

	public void setupOnClickListeners() {
        
		radio05min.setOnClickListener(this);
		radio10min.setOnClickListener(this);
		radio15min.setOnClickListener(this);
		timesetupButton.setOnClickListener(this);
    }
    
    public void onClick(View V) {
    	
    	switch(V.getId()) {
    	
	    	case R.id.time_setup_button:

				pd = ProgressDialog.show(this, "Creating a new game", "Please wait...", true, false);
            	Thread cThread = new Thread(this);
            	cThread.start();

	    		break;
	    		
	    	case R.id.rbtn_time_05_min:
	    		// needs to pass the checked option
	    		Log.d("TIME SELECT", "10 min");
	    		time = 10;
	    		break;
	    		
	    	case R.id.rbtn_time_10_min:
	    		// needs to pass the checked option
	    		Log.d("TIME SELECT", "20 min");
	    		time = 20;
	    		break;
	    		
	    	case R.id.rbtn_time_15_min:
	    		// needs to pass the checked option
	    		Log.d("TIME SELECT", "30 min");
	    		time = 30;
	    		break;

    	}

    }

	@Override
	public void run() {

		answer = Connection.createGame(username, time);
		
		if(answer != -1) {
		    
		    preferences.edit().putInt("idgame", answer).commit();
		    
			// if the game was successfully created
			// join yourself to the same game
			// and save the gameSessionID in the sharedPreferences
		    
			int joined = Connection.joinGame(username, answer);
			
			Log.d("S GAME PHASE", answer + "");
			
			Connection.setGamePhase(answer, Utils.CREATED);
            
			preferences.edit().putInt("gameSessionID", joined).commit();
			
			if(joined != -1) {
				
				// sets the game phase for wait players
				Connection.setGamePhase(answer, Utils.WAITING_PLAYERS);
				
				handler.sendEmptyMessage(1);
			}
		} else {
			handler.sendEmptyMessage(0);
		}
		
		
	}
	
	private final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			
			if (msg.what == 1) {
				setPrefsHost(); // stores that the player is a host
				pd.dismiss();
				Utils.showToastShort(getBaseContext(), "New game created");
				
				Intent gotoLocate = new Intent(getBaseContext(), locate.class);
	    		startActivity(gotoLocate);
				
			} else {
				pd.dismiss();
				Utils.showToastShort(getBaseContext(), "We are sorry, an error has occurred");
			}

		}
	};
    
}